/**
 * 封装sendRequest请求
 * @param {Object} url	请求路径
 * @param {Object} data	请求参数	
 * @param {Object} method	请求方法默认为POST
 * @param {Object} header	头部信息默认为application/x-www-form-urlencoded;charset=UTF-8
 */
import config from './config.js'
const SendUrl = config.Url
function sendRequest(url, param = {}, method = "POST", header = "application/x-www-form-urlencoded;charset=UTF-8") {
	uni.showLoading({
		title: '加载中...',
	})
	return new Promise(function(resolve, reject) {
		uni.request({
			url: SendUrl + url,
			data: param,
			method: method,
			dataType: 'json',
			header: {
				'content-type': 'application/x-www-form-urlencoded'
			},
			success: (res) => {	
				uni.hideLoading();
				console.log('res.data.code------',res.data.code)
				if (res.data.code == 0) {
					resolve(res);
				}else if(res.data.code == 401){
				   uni.removeStorageSync('token')
				  // uni.reLaunch({
				  // 	url: '/pages/login/disembark/disembark'
				  // })   
				}else if(res.data.code == -1){
				   uni.removeStorageSync('token')
				  // uni.reLaunch({
				  // 	url: '/pages/login/disembark/disembark'
				  // })   
				} else {
			       resolve(res);
				}
			},
			fail: (err) => {
				console.log('123',err)
				uni.hideLoading();
				reject(err)
			}
		})
	})

}

export default {
	sendRequest
}
